package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.AttachmentManager;
import com.xyzh05.service.MajorManager;
import com.xyzh05.service.MaterialInfoManager;
import com.xyzh05.service.MaterialManager;
import com.xyzh05.service.SchoolManager;
import com.xyzh05.service.VipServiceManager;
import com.xyzh05.util.PageBean;
import com.xyzh05.model.Attachment;
import com.xyzh05.model.Material;
import com.xyzh05.model.MaterialInfo;
import com.xyzh05.model.VipService;
import com.xyzh05.Constants;

import java.util.Date;
import java.util.List;

public class MaterialAction extends UploadFileAction implements Preparable {
	private static final long serialVersionUID = -12312312312321222L;
	
	private MaterialInfoManager materialInfoManager;
    private MajorManager majorManager; 
    private VipServiceManager vipServiceManager;
    private AttachmentManager attachmentManager;
    private SchoolManager schoolManager; 
    private MaterialManager materialManager;
    private List<Material> materials;
    private Material material;
    private VipService vipService;
	private MaterialInfo materialInfo;
    private Long id;
    private String query;
	private Long vipServiceId;
    private PageBean pageBean;
	private Integer resultCode;
	private String status;
	private Long attachtId;

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
    }


    public String list() {
    	 pageBean = materialManager.findList(pageBean, material);
         return SUCCESS;
    }

    public String delete() {
        materialManager.remove(material.getId());
        saveMessage(getText("material.deleted"));

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (material.getId() == null);

        materialManager.save(material);

        String key = (isNew) ? "material.added" : "material.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
    public String initEditMaterialCheck() {
    	if (id != null){
    		material = materialManager.get(id);
    	}else{
    		material = new Material();
    	}
    	
        return SUCCESS; 
    }
    
    public String editMaterialCheck() {    	
    	try {
    		Material m = materialManager.get(material.getId());
    		
    		m.setScoreCheck(material.getScoreCheck());
    		m.setReadingCheck(material.getReadingCheck());
    		m.setCvCheck(material.getCvCheck());
    		m.setEssayCheck(material.getEssayCheck());
    		m.setGtCheck(material.getGtCheck());
    		m.setHonourCheck(material.getHonourCheck());
    		m.setInstituteCheck(material.getInstituteCheck());
    		m.setPsCheck(material.getPsCheck());
    		m.setMaterialListCheck(material.getMaterialListCheck());
    		m.setRankingCheck(material.getRankingCheck());
    		m.setRlCheck(material.getRlCheck());
    		m.setAssetsCheck(material.getAssetsCheck());
    		
			materialManager.saveOrUpdate(m);
			resultCode = 1;			
		} catch (Exception e) {
			resultCode = 0;
		}
    	return SUCCESS;
    }

    public String materialOutline(){
    	materials = materialManager.doSelSchool(vipServiceId);
    	return SUCCESS;
    }
    
    public String initMaterialCheck(){
		vipService = vipServiceManager.get(vipServiceId);
		
		Material material = new Material();
		material.setVipService(vipService);
		
		materialInfo = new MaterialInfo();
		materialInfo.setMaterial(material);

		pageBean = materialInfoManager.findList(pageBean, materialInfo);
    	return SUCCESS;
    }
    
    public String initApplyResult(){
		vipService = vipServiceManager.get(vipServiceId);
		
		Material material = new Material();
		material.setVipService(vipService);
		
		materialInfo = new MaterialInfo();
		materialInfo.setMaterial(material);

		pageBean = materialInfoManager.findList(pageBean, materialInfo);
    	return SUCCESS;
    } 
    
    public String editApplyResult(){
    	try {
        	material = materialManager.get(id);
        	material.setStatus(Short.valueOf(status));
        	materialManager.saveOrUpdate(material);
        	resultCode = 1;
		} catch (Exception e) {
        	resultCode = 0;
		}
    	return SUCCESS;
    }
    
    /**
     * 跳转到上传页面
     * @return
     */
    public String toUploadAttach(){
    	//查询是否有附件id
    	List<Attachment> list = attachmentManager.findAttachments(id, Constants.SEVEN);
    	attachtId = list.size() != 0 ? list.get(0).getId() : null;    	
    	return SUCCESS;
    }
    
    /**
     * 上传文书终稿
     * @return
     */
    public String uploadAttachment(){
    	try {
			// 上传文件
			super.uploadFiles();
			
			Attachment attachment = null;
			if(attachtId == null){
				attachment = new Attachment();
			}else{
				attachment = attachmentManager.get(attachtId);			
			}			
			attachment.setName(uploadFileName[0]);
			attachment.setPath(targetPath[0]);	
			attachment.setSize(Long.valueOf(fileSize[0]));
			attachment.setUploadTime(new Date());
			attachment.setUploadUser(getCurrentUser().getId());
			attachment.setType(uploadContentType[0]);
			attachment.setOwnerId(id);
			attachment.setOwnerType(Constants.SEVEN);			
			attachmentManager.save(attachment);
			Material m = materialManager.get(id);
			m.setFinalCheck(Constants.MATERIAL_ONE);
			materialManager.save(m);
			getRequest().setAttribute("oId", id);
			getSession().setAttribute("uploadStatus", true);
		} catch (Exception e) {
			getSession().setAttribute("uploadStatus", false);
			log.error("附件上传失败!", e);
		}
		return SUCCESS;
    }
    
	public MaterialInfoManager getMaterialInfoManager() {
		return materialInfoManager;
	}

	public void setMaterialInfoManager(MaterialInfoManager materialInfoManager) {
		this.materialInfoManager = materialInfoManager;
	}

	public MajorManager getMajorManager() {
		return majorManager;
	}

	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public VipServiceManager getVipServiceManager() {
		return vipServiceManager;
	}

	public void setVipServiceManager(VipServiceManager vipServiceManager) {
		this.vipServiceManager = vipServiceManager;
	}

	public SchoolManager getSchoolManager() {
		return schoolManager;
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}

	public MaterialManager getMaterialManager() {
		return materialManager;
	}

	public void setMaterialManager(MaterialManager materialManager) {
		this.materialManager = materialManager;
	}

	public void setAttachmentManager(AttachmentManager attachmentManager) {
		this.attachmentManager = attachmentManager;
	}

	public AttachmentManager getAttachmentManager() {
		return attachmentManager;
	}

	public List<Material> getMaterials() {
		return materials;
	}

	public void setMaterials(List<Material> materials) {
		this.materials = materials;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public MaterialInfo getMaterialInfo() {
		return materialInfo;
	}

	public void setMaterialInfo(MaterialInfo materialInfo) {
		this.materialInfo = materialInfo;
	}

	public Long getVipServiceId() {
		return vipServiceId;
	}

	public void setVipServiceId(Long vipServiceId) {
		this.vipServiceId = vipServiceId;
	}

	public VipService getVipService() {
		return vipService;
	}

	public void setVipService(VipService vipService) {
		this.vipService = vipService;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	public Long getAttachtId() {
		return attachtId;
	}

	public void setAttachtId(Long attachtId) {
		this.attachtId = attachtId;
	}

}